package com.aier.classes.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassPhoto;
import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.common.CityUtil;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.school.entity.School;

@Scope("prototype")
@Component("classesCommonAction")
public class ClassesCommonAction extends ClassesBaseAction {

    /**
     * 班级公共元素
     * 
     * @param 班级管理
     *            -班级左侧栏页面与班级名称栏
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;

    private Long cid;
    private Long tid;

    private String img; // 班级主图
    private File file;
    private String photoName; // 照片原名

    private int s; // 成功:1 ,失败:0
    private String error_info; // 错误提示信息

    private Classes classes; // 班级
    private School school;// 学校

    private String selCoId;// 默认图名称
    
    private List<ClassPhoto> cpList; // 班级相册图片列表

    @Override
    public String execute() {
        classes = classService.searchClassById(cid);
        school = classService.searchSchoolByCid(cid);
        if (school.getCid() != null) {
            school.setAddress(CityUtil.getCityListToString(school.getCid()));
        }
        return SUCCESS;
    }

    // 上传班级封面图片
    public String updateClassImg() {
        img = ImageUtil.uploadImageAdjustable(file, classimg_upload_url,
                photoName, Constant.num_269, Constant.num_82, Constant.num_980,
                Constant.num_300, true);

        return SUCCESS;
    }

    /**
     * 删除班级封面图片
     * 
     * @return
     */
    public String deleteImage() {
        File fo = new File(classimg_upload_url + photoName);
        if (fo.exists()) {
            fo.delete();
        }
        s = 1;
        return SUCCESS;
    }

    // 将班级封面图片名存入数据库
    public String updateSImgName() {
        if (classService.updateClassImg(cid, img) == 0) {
            s = 0;
            error_info = "修改班级封面失败";
        }
        s = 1;
        return SUCCESS;
    }

    // 设置给定图片为封面
    public String updateGivImgName() {

        if (classService.updateClassImg(cid, selCoId + ".jpg") == 0) {
            s = 0;
            error_info = "修改班级封面失败";
        }
        s = 1;
        return SUCCESS;
    }

    // 读取班级封面图片
    public String serachClassimg() {
        Classes c = classService.searchClassById(cid);
        if (null != c) {
            img = c.getImg();
        }
        return SUCCESS;
    }

    // 查询相册照片
    // 查询某相册下的图片列表
    public String searchClassPhotoImg() {
        cpList = classService.searchClassPhotoList(cid);
        return SUCCESS;
    }

    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public Classes getClasses() {
        return classes;
    }

    public String getSelCoId() {
        return selCoId;
    }

    public void setSelCoId(String selCoId) {
        this.selCoId = selCoId;
    }

    public School getSchool() {
        return school;
    }

    public List<ClassPhoto> getCpList() {
        return cpList;
    }

}